package Starter;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;

import javax.imageio.ImageIO;
import javax.swing.*;

import Components.ImageButton;
import Connector.Connector;

/**
 * Klasa reprezentujaca funkcje logowania/wej�cia do serweru
 * @author Maciej Graszek
 * @author Paweł Rychły
 *
 */

public class LogIn extends JPanel {
	
	/* Elementy okna logowania */
	private JLabel login;
	private ImageButton start;
	private ImageButton cancel;
	private JTextField logName;
	private JDialog currentDialog;
	
	/* T�o */
	private Image background;
	
	/* Parametry okna */
	private int width = 342;
	private int heigh = 350;
	
	/* Akcje */
	private String IN = "in";
	private String OUT = "out";

	/* Folder */
	private String DIR = "picture/logon_pic/";
	
	public LogIn(JDialog jf) {

		this.setLayout(null);
		currentDialog = jf;
		this.setSize(width, heigh);
		
		try {
			background = ImageIO.read(new File(DIR+"beth_tetris.jpg"));
		} catch (IOException e) {
			System.out.println("Nie za�adowano t�a");
			e.printStackTrace();
		}
		
		//repaint();		
		initComponents();
		
	}
	
	/**
	 * Metoda inicjuj�ca komponenty na palecie
	 */
	private void initComponents() {
		
		login = new JLabel();
		logName = new JTextField("Nickname");

			cancel = new ImageButton(DIR+"exit_UP.png",DIR+"exit_DOWN.png");			
			start = new ImageButton(DIR+"start_UP.png",DIR+"start_DOWN.png");
	
		
		logName.setBounds(160,10,70,20);
	
		start.setBounds(10,10,51,45);
		
		login.setText("Podaj nick: ");
		login.setBounds(90,10,70,20);	
		
		cancel.setBounds(283,10,31,29);
	
		start.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                openMenu(evt);
            }
        });
		
		cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                System.exit(-1);
            }
        });
		
        
        this.add(login);
        this.add(start);
        this.add(cancel);
        this.add(logName);       
	}
	
    public void openMenu(ActionEvent e) {

        	System.out.println("Wchodzimy do gry!");
        	
            java.awt.EventQueue.invokeLater(new Runnable() {

                public void run() {
                	
                	//TODO Po��czenie z serwerem
                	
                	int id = 5;
                	
                    JDialog frame = new JDialog();
                    frame.setTitle("Smash-Trish Menu");
                    
                    //frame.setUndecorated(true);
                    frame.setMinimumSize(new Dimension(707, 530)); 
                    
   	
                	final Menu menu = new Menu(login.getText(), id, frame);
                    menu.setOpaque(true);                
                    
                    frame.setContentPane(menu);
                    frame.pack();
                    frame.setVisible(true);
                	currentDialog.setEnabled(false);
                	currentDialog.setVisible(false);
                }
            });	        	

    }	
	
    /**
     * Metoda rysujaca (g��wnie) t�o
     */
	@Override
	public void paintComponent(Graphics g) {
		
	    Graphics2D g2 = (Graphics2D) g;
	    
	    g2.drawImage(background, 0, 0, null);
	    
	}

	/**
	 * Metoda tworz�ca okno
	 * TODO resizeable = false
	 */
    private static void createAndShowGUI() {

    	/* Dialog, poniewaz mozna usunac gorny pasek od okna gry */
        JDialog frame = new JDialog();
        
        /* Metoda do wy��czenia paska, opcjonalna */
        //frame.setUndecorated(true);
        //frame.setMinimumSize(new Dimension(325, 310));      
        frame.setMinimumSize(new Dimension(330, 340));
        
        frame.setResizable(false);

        final LogIn newContentPane = new LogIn(frame);
        //newContentPane.setOpaque(true); 
        frame.setContentPane(newContentPane);

        frame.pack();
        frame.setVisible(true);

    }
    
	/**
	 * @param args
	 */
	public static void main(String[] args) {

        java.awt.EventQueue.invokeLater(new Runnable() {
 
            public void run() {
            	createAndShowGUI();
            	try {
					Connector con = new Connector();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            } 
        });		

	}


}